package nccloud.web.pu.buyingreq.action;

import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.web.cmp.informer.action.InformerAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.pf.pub.PfDataCache;
import nc.itf.uap.IUAPQueryBS;
import nc.itf.uap.pf.IPfExchangeService;
import nc.itf.uap.pf.IplatFormEntry;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.fi.pub.SqlUtils;
import nc.vo.ic.m4n.entity.TransformVO;
import nc.vo.pf.pub.util.SQLUtil;
import nc.vo.pu.m20.entity.PraybillHeaderVO;
import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pu.m422x.entity.StoreReqAppHeaderVO;
import nc.vo.pu.m422x.entity.StoreReqAppItemVO;
import nc.vo.pu.m422x.entity.StoreReqAppVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.billtype.BilltypeVO;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.web.scmpub.pub.action.TempletQueryAction;



//物资需求申请单-请购单获取信息
public class GetStoreReqInfo  extends InformerAction implements ICommonAction {

	private IUAPQueryBS qService;
	
	
	public IUAPQueryBS getQService() {
		if(qService == null) {
			qService = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		}
		return qService;
	}


	public Object doAction(IRequest request) {
	    String str = request.read();
	
	    JSONObject json = JSONObject.parseObject(str);
	              //获取数据           
	    String params = json.get("pks").toString();
	    String[] pks = params.split(",");
	    
	    JSONArray selectdatas = (JSONArray) json.get("selectdatas");
	    
	    Map<String,JSONObject> datamaps = new HashMap<>();
	    for (int i = 0; i < selectdatas.size(); i++) {
	    	JSONObject selectdata = (JSONObject) ((JSONObject) selectdatas.get(i)).get("values");
	    	JSONObject pkdata = (JSONObject) selectdata.get("pk_storereq_b");
	    	datamaps.put(pkdata.get("value").toString(), selectdata);
		}

	    
	    try {
	    	//根据表体主键找出对应的物资需求申请单  主子信息  
	    	String inSql = SQLUtil.buildSqlForIn("pk_storereq_b", pks);
	    	List<StoreReqAppItemVO> sbvos = (List<StoreReqAppItemVO>) getQService().retrieveByClause(StoreReqAppItemVO.class,inSql );
	    	Map<String,StoreReqAppItemVO> map = new HashMap<>();
	    	for(StoreReqAppItemVO sbvo : sbvos) {
	    		map.put(sbvo.getPk_storereq_b(), sbvo);
	    	}
	    	String pk_storereq = sbvos.get(0).getPk_storereq();
	    	StoreReqAppHeaderVO shvo = (StoreReqAppHeaderVO) getQService().retrieveByPK(StoreReqAppHeaderVO.class, pk_storereq);
	    	if(shvo.getFbillstatus() != 3) {
	    		throw new BusinessException("只能操作审批通过的单据！");
	    	}
	    	StoreReqAppVO sVO= new StoreReqAppVO();
	    	sVO.setParentVO(shvo);
	    	sVO.setChildrenVO(sbvos.toArray(new StoreReqAppItemVO[0]));
	    	
	    	InvocationInfoProxy.getInstance().setGroupId(shvo.getPk_group());//重新设置一下集团
	    	
			IPfExchangeService service = NCLocator.getInstance().lookup(IPfExchangeService.class); 
			PraybillVO fkVO = (PraybillVO) service.runChangeData("422X", "20", sVO,null);
			//请购数量重新赋值
			PraybillItemVO[] pbvos = fkVO.getBVO();
			for (int i = 0; i < pbvos.length; i++) {
				String csourcebid = pbvos[i].getCsourcebid();
				StoreReqAppItemVO sbvo = map.get(csourcebid);
				UFDouble sqsl = sbvo.getNnum().sub(obj2UFDouble(sbvo.getVbdef6()));
				JSONObject selectdata = datamaps.get(csourcebid);
				UFDouble npraynum =  new UFDouble(((JSONObject) selectdata.get("npraynum")).get("value").toString());
				
				if(sqsl.compareTo(UFDouble.ZERO_DBL) <=0) {
					throw new RuntimeException("剩余可申请数量必须大于0  请刷新重试  行号"+sbvo.getCrowno());
				}
//				if(npraynum.compareTo(sqsl) >0) {
//					throw new RuntimeException("当前申请数量超过可申请数量。请刷新重试！当前可申请数量为: "+sqsl+" 行号"+sbvo.getCrowno());
//				}
				pbvos[i].setNnum(npraynum);
				pbvos[i].setNastnum(npraynum);
				pbvos[i].setCrowno((i+1)*10+"");
				pbvos[i].setStatus(VOStatus.NEW);
			}
			
			
			//lxiang20240112 生成请购单不再跳转 改为后台自动生成
//	        String templetId = TempletQueryAction.getTempletIdByPageCode("400400400_card");
//	        BillCardOperator oper = new BillCardOperator(templetId, "400400400_card");
//	        oper.setTransFlag(false);
//	        BillCard card = oper.toCard(fkVO);
//	        return card;
			
			fkVO.getParent().setStatus(VOStatus.NEW);
			IplatFormEntry service2 = NCLocator.getInstance().lookup(IplatFormEntry.class); 
			PraybillVO[] prayVOs = (PraybillVO[]) service2.processAction("SAVEBASE", "20",
    				  null, fkVO, null, null); 
			
			prayVOs = (PraybillVO[]) service2.processAction("APPROVE", "20",
  				  null, prayVOs[0], null, null); 
			
    		return true;
	    } catch (Exception e) {
	        ExceptionUtils.wrapException(e);
	        return null;
	    }
	}
	
	private UFDouble obj2UFDouble(Object obj){
		return obj == null ? UFDouble.ZERO_DBL:new UFDouble(obj.toString());
	}
	

}
